package com.anXin.user.controller;


import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.user.dtos.LoginByPhoneDto;
import com.anXin.model.user.dtos.LoginDto;
import com.anXin.model.user.dtos.PhoneCodeDto;
import com.anXin.model.user.dtos.RegDto;
import com.anXin.model.user.pojos.User;
import com.anXin.user.mapper.UserMapper;
import com.anXin.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "管理用户接口" )
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    @ApiOperation("获取所有用户列表")
    @GetMapping("/list")
    private ResponseResult getList(long page,long pageSize){
        Page<User> rowPage = new Page(page, pageSize);

        //queryWrapper组装查询where条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        rowPage = userMapper.selectPage(rowPage, queryWrapper);
        return ResponseResult.okResult(rowPage);
    }
    @ApiOperation("根据关键字查询用户")
    @GetMapping("/queryByKey")
    public ResponseResult queryByKey() {
        return userService.queryByKey();
    }

    @ApiOperation("根据关键字查询用户")
    @GetMapping("/queryByKey/{key}")
    public ResponseResult queryByKey(@PathVariable("key")  String key) {
        return userService.queryByKey(key);
    }

    @ApiOperation("更新用户")
    @PutMapping("/update")
    public ResponseResult update(@RequestBody User user){
        boolean update = userService.updateById(user);
        if (update)return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.USER_UPDATE_ERROR);
    }



    @ApiOperation("更改用户状态（封禁）")
    @GetMapping("/setStatus/{id}")
    public ResponseResult setStatus(@PathVariable("id")  Long id) {
        return userService.setStatus(id);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/userInfo/{id}")
    public ResponseResult userInfo(@PathVariable("id")  Long id) {
        return userService.userInfo(id);
    }
}